const User = require('./User');
const Note = require('./Note');
const NoteTag = require('./NoteTag');
const NoteImage = require('./NoteImage');
const Draft = require('./Draft');
const Like = require('./Like');
const Follow = require('./Follow');
const Video = require('./Video');
const VideoLike = require('./VideoLike');
const Comment = require('./Comment');
const MembershipOrder = require('./MembershipOrder');
const Category = require('./Category');
const NoteCategory = require('./NoteCategory');

// 关联关系
Note.hasMany(NoteTag, { foreignKey: 'note_id', onDelete: 'CASCADE' });
Note.hasMany(NoteImage, { foreignKey: 'note_id', onDelete: 'CASCADE' });
Note.hasMany(Like, { foreignKey: 'note_id', onDelete: 'CASCADE' });
Note.hasMany(Comment, { foreignKey: 'note_id', onDelete: 'CASCADE' });
Note.hasMany(NoteCategory, { foreignKey: 'note_id', onDelete: 'CASCADE' });

User.hasMany(Note, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(Draft, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(Like, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(Comment, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(Video, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(VideoLike, { foreignKey: 'user_id', onDelete: 'CASCADE' });
User.hasMany(MembershipOrder, { foreignKey: 'user_id', onDelete: 'CASCADE' });
// User和Follow的关联
User.hasMany(Follow, { as: 'followerLinks', foreignKey: 'follower_id', onDelete: 'CASCADE' });
User.hasMany(Follow, { as: 'followingLinks', foreignKey: 'following_id', onDelete: 'CASCADE' });
Follow.belongsTo(User, { as: 'followerUser', foreignKey: 'follower_id', onDelete: 'CASCADE' });
Follow.belongsTo(User, { as: 'followingUser', foreignKey: 'following_id', onDelete: 'CASCADE' });

Video.hasMany(VideoLike, { foreignKey: 'video_id', onDelete: 'CASCADE' });
Video.hasMany(Comment, { foreignKey: 'video_id', onDelete: 'CASCADE' });

Category.hasMany(NoteCategory, { foreignKey: 'category_id', onDelete: 'CASCADE' });

module.exports = {
  User,
  Note,
  NoteTag,
  NoteImage,
  Draft,
  Like,
  Follow,
  Video,
  VideoLike,
  Comment,
  MembershipOrder,
  Category,
  NoteCategory
};